package com.zoro.k8sapp.app.consumer.controller;

import com.zoro.k8sapp.app.consumer.feign.ProducerInfoFeign;
import com.zoro.k8sapp.commondto.common.BriefInfo;
import com.zoro.k8sapp.commonutil.network.JsonUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/producer-info")
public class ProducerInfoController {

    private final static HttpClient httpClient = HttpClients.createDefault();

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final ProducerInfoFeign producerInfoFeign;


    public ProducerInfoController(ProducerInfoFeign producerInfoFeign) {
        this.producerInfoFeign = producerInfoFeign;
    }

    @GetMapping
    public BriefInfo producerInfo() throws Exception {
        return this.producerInfoFeign.briefInfo();
    }

    @GetMapping("/2")
    public BriefInfo producerInfo2() throws IOException {
        HttpEntity entity = httpClient.execute(
                RequestBuilder.get("http://app-producer-service/info/brief").build()
        ).getEntity();
        InputStream is = entity.getContent();
        byte[] bytes = is.readAllBytes();
        String out = new String(bytes);
        return JsonUtil.toJavaType(out, BriefInfo.class);
    }

}
